package com.iot.mqttbroker.controller;

import com.iot.mqttbroker.commom.Result;
import com.iot.mqttbroker.service.IMQTTPublisher;
import com.iot.mqttbroker.service.IMQTTSubscriber;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Slf4j
@CrossOrigin
@RestController
public class DemoRestController {
    public static final String TOPIC_LOOP_TEST = "sub_led";

    public static final String TOPIC = "led_state";

    @Resource
    IMQTTPublisher publisher;

    @Resource
    IMQTTSubscriber subscriber;

    @PostConstruct
    public void init() {
        subscriber.subscribeMessage(TOPIC);
    }

    @PostMapping(value = "/led")
    public Result<?> index(@RequestBody String data) {
        publisher.publishMessage(TOPIC_LOOP_TEST, data);
        return Result.success();
    }
}
